﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using BLL;
using GUI.UserInterfaces;
using GUI.Utilities;
using CTRL;

namespace GUI.UserControls
{
    public partial class UCListDemos : UserControl
    {
        List<Record> recordList;
        CtrlDemos ctrlDemos;

        public UCListDemos()
        {
            InitializeComponent();

            ctrlDemos = CtrlDemos.Instance;
            recordList = new List<Record>();

            listPopulize();
            txtSearch.PlaceholderText = "Search for a map...";
        }

        public void listPopulize()
        {
            recordList = ctrlDemos.GetDemoList();
            PopulateList(recordList);
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.TextLength > 0)
            {
                var searchList = recordList.Where(x => x.Map.Name.Contains(txtSearch.Text)).ToList();

                PopulateList(searchList);
            }
            else
            {
                PopulateList(recordList);
            }
        }

        private void PopulateList(List<Record> l)
        {
            demoList.Items.Clear();

            foreach (var i in l)
            {
                ListViewItem li = new ListViewItem();

                if ((i.Map != null) && i.Jumper != null)
                {
                    li.Text = i.Map.Name;
                    li.SubItems.Add(UTIL.KZTime.ToString(i.Time));
                    li.SubItems.Add(i.Jumper.Name);
                    li.SubItems.Add(i.Date.ToShortDateString());
                    li.SubItems.Add(i.Beaten.ToString());

                    if (System.IO.File.Exists(System.IO.Path.Combine(UTIL.KZSettings.DemoFolder, i.File)))
                    {
                        li.BackColor = Color.LightGreen;
                    }
                }

                demoList.Items.Add(li);
            }

            demoList.AutoResizeColumns(System.Windows.Forms.ColumnHeaderAutoResizeStyle.ColumnContent);
            demoList.AutoResizeColumn(4, System.Windows.Forms.ColumnHeaderAutoResizeStyle.HeaderSize);
        }

        private void demoList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string map = demoList.SelectedItems[0].Text;
            Record record = recordList.FirstOrDefault(x => x.Map.Name.Equals(map));

            if (record.Jumper.Name.Equals("n/a") || record.Time.Equals("**:**.**"))
            {
                var message = "There is no record on this map (yet)\n";
                message += "But you can record it and submit to cosy-climbing.net...\n";
                message += "Would you? :)";

                MessageBox.Show(message, "Something went wrong here...", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (record != null)
            {
                if (System.IO.File.Exists(System.IO.Path.Combine(UTIL.KZSettings.DemoFolder, record.File)))
                {
                    MessageBox.Show("You already have that demo, why would you need it again? :)", "Something went wrong here...", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ctrlDemos.DownloadDemo(record.File);
                            
                    foreach (ListViewItem item in demoList.Items)
                    {
                        if (item.Text.ToLower().Equals(record.Map.Name.ToLower()))
                        {
                            item.BackColor = Color.LightGreen;
                        }
                    }
                }
            }
            else
            {
                var message = "There is some bug :(\n";
                message += "Please contact Nikola from cosy-climbing.net";

                MessageBox.Show(message, "Something went wrong here...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            PopulateList(recordList);
            txtSearch.Text = String.Empty;
        }
    }
}
